package cn.geminis.resources.service.controller;

import cn.geminis.resources.service.model.Organization;
import cn.geminis.resources.service.model.filter.OrganizationFilter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author Allen
 */
public class OrganizationController {

    @RequestMapping(value = {"children/", "children/{parentId}"}, method = RequestMethod.GET)
    public List<Organization> children(@PathVariable(required = false) String parentId) {
        var filter = OrganizationFilter.builder();
        if (StringUtils.hasText(parentId)) {
            filter.parentId(parentId);
        } else {
            filter.parentIdIsNull(true);
        }

        return this.repository.findAll(filter.build());
    }

}
